package com.zlt.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;

public class StringUtil {

	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isNotNull(String str) {
		return str != null && !"".equals(str);
	}
	
	private static String string = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
	
	/**
	 * 随机生成指定长度的字符串
	 * @param len
	 * @return
	 */
	public static String randomStr (int len) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < len; i++) {
			int index = (int) (Math.random() * string.length());
			builder.append(string.charAt(index));
		}
		return builder.toString();
	}
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getCurrentTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(new Date());
	}

	/**
	 * 生成一个uuid的字符串
	 * @return
	 */
	public static String uuid (){
		return UUID.randomUUID().toString();
	}


	/**
	 * 将图片加密变成字符串
	 * @param image
	 * @return
	 */
	public static String changeImageToString (BufferedImage image){
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		try {
			ImageIO.write(image,"jpg",byteArrayOutputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String s = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
		return "data:image/jpg;base64," + s;
	}
}
